#coding:utf-8


import buss
import datamethod as dm

# 图片抓拍请求
class Protocol4101(buss.BaseProtocol):
    type = [0x41, 0x01]
    name = u"图片抓拍请求"
    
    dp = {u'352×288' : 0x01,
          u'176×144' : 0x02,
          u'720×576' : 0x03,
        }
        
    dq = {u'质量高' : 0x01,
         u'质量中' : 0x02,
         u'质量低' : 0x03,
        }
        
    def init(self):
        self.resolution = 0x01
        self.resource = 0x01
        self.quality = 0x01
        self.numbers = 0x01
        self.setting = [0x08] * 5
        
    def separate(self, data):
        argCount = data.get(1)
        for i in range(argCount):
            argType = data.get(1)
            argLength = data.get(1)
            argument = data.get(argLength)
            if argType == 0x01:
                self.resolution = argument
            elif argType == 0x02:
                self.resource = argument
            elif argType == 0x03:
                self.quality = argument
            elif argType == 0x04:
                self.numbers = argument
            elif argType == 0x06:
                self.setting = argument
    
    def assemble(self, data):
        argCount = 5
        data.set(argCount)
        data.set(0x01, 0x01, self.resolution)
        data.set(0x02, 0x01, self.resource)
        data.set(0x03, 0x01, self.quality)
        data.set(0x04, 0x01, self.numbers)
        data.set(0x06, 0x05, self.setting)
        
    def getResolving(self):
        return dm.getKey(self.dp, self.resolution)
    
    def setResolving(self, value):
        self.resolution = self.dp[value]
        
    def getCamera1(self):
        return dm.extract(self.resource, 7, 7)
    
    def setCamera1(self, value):
        self.resource = dm.insert(self.resource, int(value), 7, 7)
        
    def getCamera2(self):
        return dm.extract(self.resource, 6, 6)
    
    def setCamera2(self, value):
        self.resource = dm.insert(self.resource, int(value), 6, 6)
        
    def getCamera3(self):
        return dm.extract(self.resource, 5, 5)
    
    def setCamera3(self, value):
        self.resource = dm.insert(self.resource, int(value), 5, 5)
        
    def getCamera4(self):
        return dm.extract(self.resource, 4, 4)
    
    def setCamera4(self, value):
        self.resource = dm.insert(self.resource, int(value), 4, 4)
        
    def getQuality(self):
        return dm.getKey(self.dq, self.quality)
            
    def setQuality(self, value):
        self.quality = self.dq[value]
        
    def getNumbers(self):
        return self.numbers
    
    def setNumbers(self, value):
        self.numbers = int(value)
        
    def getBrightness(self):
        return self.setting[0]
    
    def setBrightness(self, value):
        self.setting[0] = int(value)
        
    def getContrast(self):
        return self.setting[1]
    
    def setContrast(self, value):
        self.setting[1] = int(value)
        
    def getTinge(self):
        return self.setting[2]
    
    def setTinge(self, value):
        self.setting[2] = int(value)
        
    def getBlueSaturation(self):
        return self.setting[3]
    
    def setBlueSaturation(self, value):
        self.setting[3] = int(value)
    
    def getRedSaturation(self):
        return self.setting[4]
    
    def setRedSaturation(self, value):
        self.setting[4] = int(value)
        
    def getList(self):
        return [
                [u'分辨率', self.dp.keys(), self.getResolving, self.setResolving],
                [u'摄像一', ['0','1'], self.getCamera1, self.setCamera1],
                [u'摄像二', ['0','1'], self.getCamera2, self.setCamera2],
                [u'摄像三', ['0','1'], self.getCamera3, self.setCamera3],
                [u'摄像四', ['0','1'], self.getCamera4, self.setCamera4],
                [u'图片质量', self.dq.keys(), self.getQuality, self.setQuality],
                [u'单次抓拍张数', (1, 10), self.getNumbers, self.setNumbers],
                [u'亮度', (1, 15), self.getBrightness, self.setBrightness],
                [u'对比度', (1, 15), self.getContrast, self.setContrast],
                [u'色调', (1, 15), self.getTinge, self.setTinge],
                [u'蓝色饱和度', (1, 15), self.getBlueSaturation, self.setBlueSaturation],
                [u'红色饱和度', (1, 15), self.getRedSaturation, self.setRedSaturation],
                ]
        
# 4110图片数据传输帧应答
class Protocol4110(buss.BaseProtocol):
    type = [0x41, 0x10]
    name = u"图片数据传输帧应答"
    dr = {u'本窗口包全正确' : 0x01,
          u'本窗口包有错误' : 0x02,
         }
    def init(self):
        self.imageIndex =[0x00, 0x00]
        self.curPackage = [0x00, 0x00]
        self.lackPackage = []
        
    def separate(self, data):
        self.imageIndex = data.get(2)
        self.curPackage = data.get(2)
        response = data.get(1)
        if response != 0x01:
            lackCount = data.get(1)
            self.lackPackage = [data.get(2) for i in range(lackCount)]
            
    
    def assemble(self, data):
        data.set(self.imageIndex, self.curPackage)
        if self.lackPackage:
            data.set(0x02, len(self.lackPackage))
            for package in self.lackPackage:
                data.set(package)
        else:
            data.set(0x01)
            
    def getImageIndex(self):
        return dm.data2num(self.imageIndex)
            
    def setImageIndex(self, value):
        l = dm.num2data(value)
        self.imageIndex = [0x00] * (2-len(l)) + l
    
    def getPackageNumber(self):
        return dm.data2num(self.curPackage)
    
    def setPackageNumber(self, value):
        l = dm.num2data(value)
        self.curPackage = [0x00] * (2-len(l)) + l
    
    def getLackPackage(self):
        return [dm.data2num(p) for p in self.lackPackage]
    
    def setLackPackage(self, value):
        p = [dm.num2data(i) for i in value]
        self.lackPackage = [([0x00] * (2-len(i)) + i) for i in p]
            
# 当前窗口超时重传帧应答
class Protocol4112(Protocol4110):
    type = [0x41, 0x12]
    
# 4141图片抓拍应答 
class Protocol4141(buss.BaseProtocol):
    type = [0x41, 0x41]
    name = u"图片抓拍应答"
    dr = {u'成功' : 0x01,
          u'失败' : 0x02,
         }
    def init(self):
        self.response = 0x01
        
    def separate(self, data):
        self.response = data.get(1)
        
    def assemble(self, data):
        data.set(self.response)
    
    def getResponse(self):
        if self.response == 0x01:
            return True
        else:
            return False

# 4150图片数据传输    
class Protocol4150(buss.BaseProtocol):
    type = [0x41, 0x50]
    name = u'图片数据传输'
    df ={u'表示该图片是中心实时抓拍类型': 0x00,
         u'表示该图片是车台事件触发主动上传类型': 0x01,
         }
    dv = {u'无需确认' : 0x00,
          u'需要确认': 0x01,
          }
    def init(self):
        self.flag = 0x01
        self.imageIndex = [0x00, 0x00]
        self.packageNumbers = [0x00, 0x00]
        self.curPackage = [0x00, 0x00]
        self.start = [0x00] * 6
        self.lng = [0x00] * 4
        self.lat = [0x00] * 4
        self.resolution = 0x01
        self.quality = 0x01
        self.event = 0x01
        self.channel = 0x00
        self.dataLength = [0x00, 0x00]
        self.data = []
        
    def separate(self, data):
        p = dm.pickUp(data.get(1))
        self.responsed = p.get(1,1)
        self.imageIndex = data.get(2)
        self.packageNumbers = data.get(2)
        self.curPackage = data.get(2)
        if self.curPackage == [0x00, 0x00]:
            self.start = data.get(6)
            self.lng = data.get(4)
            self.lat = data.get(4)
            [self.resolution, self.quality, self.event, self.channel] = \
                [data.get(1) for i in range(4)]
        self.dataLength = data.get(2)
        self.data = data.get(dm.data2num(self.dataLength))
        
    def assemble(self, data):
        data.set(self.flag, self.imageIndex, self.packageNumbers,
                 self.curPackage)
        if self.curPackage == [0x00, 0x00]:
            data.set(self.start, self.lng, self.lat, self.resolution,
                     self.quality, self.event, self.channel)
        data.set(self.dataLength)
        data.set(self.data)
        
    def getImageIndex(self):
        return dm.data2num(self.imageIndex)
        
    def getImageData(self):
        return self.data
    
    def getDataLength(self):
        return self.dataLength[0]*256+self.dataLength[1]
    
    def setImageData(self, value):
        self.data = value
        length = dm.num2data(len(self.data))
        self.dataLength = [0x00]*(2-len(length)) + length
        
    def getPackageSum(self):
        return dm.data2num(self.packageNumbers)
    
    def getPackageNumber(self):
        return dm.data2num(self.curPackage)
    
    def needResponse(self):
        return bool(self.responsed)

        
# 4152    
class Protocol4152(buss.BaseProtocol):
    type = [0x41, 0x52]
    def init(self):
        self.flag = 0x00
        self.imageIndex = [0x00, 0x00]
        self.packageNumbers = [0x00, 0x00]
        self.curPackage = [0x00, 0x00]
        self.id = 0x00
        
    def separate(self, data):
        self.flag = data.get(1)
        self.imageIndex = data.get(2)
        self.packageNumbers = data.get(2)
        self.curPackage = data.get(2)
        self.id = data.get(1)
        
    def assemble(self, data):
        data.set(self.flag, self.imageIndex, self.packageNumbers,
                 self.curPackage, self.id)
        
    def getImageIndex(self):
        return dm.data2num(self.imageIndex)
        
    def getPackageSum(self):
        return dm.data2num(self.packageNumbers)
    
    def getPackageNumber(self):
        return dm.data2num(self.curPackage)
    
    

operations = [Protocol4101,]
#__all__ = [Protocol4101, Protocol4110, Protocol4112, Protocol4141, Protocol4150, Protocol4152]